<template>
  <scroll-view class="flex-col page mine"  @scroll="onPageScroll">
    <view class="flex-col section" :style="{ paddingTop: navHeight + 'px' }">
      <xlm-top-nav :scrollTop="scrollTop" title="运动员" />
      <!-- 运动员列表 -->
      <scroll-view scroll-y class="playerList">
        <view class="item" v-for="(item,index) in list" :key="index">
          <image class="avatar" :src=" item.avatar ? item.avatar : imgUrl + 'avatar1@2x.png'" />
          <text class="name">{{item.username}}</text>
        </view>
      </scroll-view>
    </view>
    <button class="mt-24 flex-col justify-start items-center text-wrapper w-full" @click="navigateTo('addPlayer')">
      <text class="text">新增运动员</text>
    </button>
  </scroll-view>
</template>
<script setup lang="ts">
  import { ref, computed } from 'vue'
  import { useOutsideAppStore } from '@/store/app'
  import { imgUrl } from '@/utils/config';
  import { onShow } from '@dcloudio/uni-app';
  import { useRequest } from '@/api';
  const appStore = useOutsideAppStore()
  const navHeight = computed(() => appStore.navHeight)
  const scrollTop = ref(0)
  const onPageScroll = (e) => {
    scrollTop.value = e.detail.scrollTop
  }
  const { API_MEMBER_LISTATHLETE_POST } = useRequest()
  const items = ref([1, 2, 3, 4, 5, 6])
  const navigateTo = (page) => {
    // 小程序获取当前页面路由
    const pages = getCurrentPages(); // 获取当前页面栈
    const currentPage = pages[pages.length - 1]; // 获取当前页面
    const currentPath = currentPage.route; // 获取当前页面的路径
    console.log('currentPath>>>', currentPath);
    if (currentPath !== 'pages/mine/addPlayer/index') {
          uni.navigateTo({
            url: '/pages/mine/addPlayer/index',
          })
        }

  };
  const list = ref([
    { name: '吴涛1' },
    { name: '吴涛2' },
    { name: '吴涛3' },
    { name: '吴涛4' },
    { name: '吴涛5' },
    { name: '吴涛6' },
    { name: '吴涛7' },
    { name: '吴涛8' },
    { name: '吴涛9' },
    { name: '吴涛10' },
    { name: '吴涛11' }
  ]) ;
  onShow(() => {
    getList();
  })
  const getList = async () => {
    let params = {
      page: 1,
      page_size: 10,
      lng:uni.getStorageSync('longitude'),
      lat:uni.getStorageSync('latitude')
    }
    list.value = [];
    try {

      const res = await API_MEMBER_LISTATHLETE_POST(params)
      console.log('getList', res)

      if (res.code === 1) {
        if (res.data.data.length > 0) {
          list.value = res.data.data;
        } else {
          list.value = [];
        }
      } else {
        uni.showToast({ title: res.msg, icon: 'none' })
      }
    } catch (error) {
      uni.showToast({ title: error.message, icon: 'none' })
    }
  }

</script>
<style lang="scss" scoped>
  @import './index.scss';
</style>